import behaviors from "../../../../../../config/behaviors";

import UserModel from "../../../../../../models/User";
const userModel = new UserModel()

Component({
  behaviors: [behaviors],
  properties: {
    image: Object,
    // 地区
    addressList: {
      type: Array,
      value: [],
      observer: function (a, b) {
        // console.log(a)
        if (a.length > 0) {
          this.setData({
            first: a[0].name
          })
        }
      }
    },
    city: String
  },
  data: {
    show: false,
    first: ''
  },
  lifetimes: {
    attached() {
      // 地区获取
      const token = userModel.getToken()
      if (token) {
        userModel.hasSupplierAddress().then(response => {
          const { data } = response
          // console.log(data)
          wx.setStorageSync('Address_id', data.address_id)
        })
      }
    }
  },
  methods: {
    handleClick() {
      wx.navigateTo({
        url: '/sub_common/pages/search/index'
      })
    },
    handleOpen() {
      if (this.properties.city) {
        return
      }
      this.setData({
        show: !this.data.show
      })
    },
    click(item) {
      const data = item.currentTarget.dataset.item
      this.setData({ show: false, first: data.name })

      const go = () => {
        userModel.saveSupplierAddress({
          address_id: data.id
        }).then(() => {
          // 保存到全局
          // getApp().globalData.Address_id = data.id
          wx.setStorageSync('Address_id', data.id)
          wx.showToast({
            title: `您选择了${data.name}城市`,
            icon: 'none'
          })
        })
      }

      // 如果没有登录
      const token = userModel.getToken()
      if (!token) {
        wx.showModal({
          title: '需要先登录,再选择地区',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/sub_main/pages/user/login/index'
              })
            } else {
              wx.showToast({
                title: '您取消了',
                icon: 'none'
              })
            }
          }
        })
      } else {
        go()
      }



    }
  }
});
